home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / vidhrdw / 1943.c < prev    next >
C/C++ Source or Header  |  2000-04-04  |  10KB  |  398 lines

  1. /***************************************************************************
  2.  
  3.   vidhrdw.c
  4.  
  5.   Functions to emulate the video hardware of the machine.
  6.  
  7. ***************************************************************************/
  8.  
  9. #include "driver.h"
  10. #include "vidhrdw/generic.h"
  11.  
  12.  
  13.  
  14. unsigned char *c1943_scrollx;
  15. unsigned char *c1943_scrolly;
  16. unsigned char *c1943_bgscrolly;
  17. static int chon,objon,sc1on,sc2on;
  18. static int flipscreen;
  19.  
  20. static struct osd_bitmap *sc2bitmap;
  21. static struct osd_bitmap *sc1bitmap;
  22. static unsigned char sc2map[9][8][2];
  23. static unsigned char sc1map[9][9][2];
  24.  
  25.  
  26.  
  27. /***************************************************************************
  28.  
  29.   Convert the color PROMs into a more useable format.
  30.  
  31.   1943 has three 256x4 palette PROMs (one per gun) and a lot ;-) of 256x4
  32.   lookup table PROMs.
  33.   The palette PROMs are connected to the RGB output this way:
  34.  
  35.   bit 3 -- 220 ohm resistor  -- RED/GREEN/BLUE
  36.         -- 470 ohm resistor  -- RED/GREEN/BLUE
  37.         -- 1  kohm resistor  -- RED/GREEN/BLUE
  38.   bit 0 -- 2.2kohm resistor  -- RED/GREEN/BLUE
  39.  
  40. ***************************************************************************/
  41. void c1943_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable,const unsigned char *color_prom)
  42. {
  43.     int i;
  44.     #define TOTAL_COLORS(gfxn) (Machine->gfx[gfxn]->total_colors * Machine->gfx[gfxn]->color_granularity)
  45.     #define COLOR(gfxn,offs) (colortable[Machine->drv->gfxdecodeinfo[gfxn].color_codes_start + offs])
  46.  
  47.  
  48.     for (i = 0;i < Machine->drv->total_colors;i++)
  49.     {
  50.         int bit0,bit1,bit2,bit3;
  51.  
  52.  
  53.         bit0 = (color_prom[0] >> 0) & 0x01;
  54.         bit1 = (color_prom[0] >> 1) & 0x01;
  55.         bit2 = (color_prom[0] >> 2) & 0x01;
  56.         bit3 = (color_prom[0] >> 3) & 0x01;
  57.         *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
  58.         bit0 = (color_prom[Machine->drv->total_colors] >> 0) & 0x01;
  59.         bit1 = (color_prom[Machine->drv->total_colors] >> 1) & 0x01;
  60.         bit2 = (color_prom[Machine->drv->total_colors] >> 2) & 0x01;
  61.         bit3 = (color_prom[Machine->drv->total_colors] >> 3) & 0x01;
  62.         *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
  63.         bit0 = (color_prom[2*Machine->drv->total_colors] >> 0) & 0x01;
  64.         bit1 = (color_prom[2*Machine->drv->total_colors] >> 1) & 0x01;
  65.         bit2 = (color_prom[2*Machine->drv->total_colors] >> 2) & 0x01;
  66.         bit3 = (color_prom[2*Machine->drv->total_colors] >> 3) & 0x01;
  67.         *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
  68.  
  69.         color_prom++;
  70.     }
  71.  
  72.     color_prom += 2*Machine->drv->total_colors;
  73.     /* color_prom now points to the beginning of the lookup table */
  74.  
  75.     /* characters use colors 64-79 */
  76.     for (i = 0;i < TOTAL_COLORS(0);i++)
  77.         COLOR(0,i) = *(color_prom++) + 64;
  78.     color_prom += 128;    /* skip the bottom half of the PROM - not used */
  79.  
  80.     /* foreground tiles use colors 0-63 */
  81.     for (i = 0;i < TOTAL_COLORS(1);i++)
  82.     {
  83.         /* color 0 MUST map to pen 0 in order for transparency to work */
  84.         if (i % Machine->gfx[1]->color_granularity == 0)
  85.             COLOR(1,i) = 0;
  86.         else
  87.             COLOR(1,i) = color_prom[0] + 16 * (color_prom[256] & 0x03);
  88.         color_prom++;
  89.     }
  90.     color_prom += TOTAL_COLORS(1);
  91.  
  92.     /* background tiles use colors 0-63 */
  93.     for (i = 0;i < TOTAL_COLORS(2);i++)
  94.     {
  95.         COLOR(2,i) = color_prom[0] + 16 * (color_prom[256] & 0x03);
  96.         color_prom++;
  97.     }
  98.     color_prom += TOTAL_COLORS(2);
  99.  
  100.     /* sprites use colors 128-255 */
  101.     /* bit 3 of BMPROM.07 selects priority over the background, but we handle */
  102.     /* it differently for speed reasons */
  103.     for (i = 0;i < TOTAL_COLORS(3);i++)
  104.     {
  105.         COLOR(3,i) = color_prom[0] + 16 * (color_prom[256] & 0x07) + 128;
  106.         color_prom++;
  107.     }
  108.     color_prom += TOTAL_COLORS(3);
  109. }
  110.  
  111.  
  112.  
  113. int c1943_vh_start(void)
  114. {
  115.     if ((sc2bitmap = osd_create_bitmap(9*32,8*32)) == 0)
  116.         return 1;
  117.  
  118.     if ((sc1bitmap = osd_create_bitmap(9*32,9*32)) == 0)
  119.     {
  120.         osd_free_bitmap(sc2bitmap);
  121.         return 1;
  122.     }
  123.  
  124.     if (generic_vh_start() == 1)
  125.     {
  126.         osd_free_bitmap(sc2bitmap);
  127.         osd_free_bitmap(sc1bitmap);
  128.         return 1;
  129.     }
  130.  
  131.     memset (sc2map, 0xff, sizeof (sc2map));
  132.     memset (sc1map, 0xff, sizeof (sc1map));
  133.  
  134.     return 0;
  135. }
  136.  
  137.  
  138. void c1943_vh_stop(void)
  139. {
  140.     osd_free_bitmap(sc2bitmap);
  141.     osd_free_bitmap(sc1bitmap);
  142. }
  143.  
  144.  
  145.  
  146. WRITE_HANDLER( c1943_c804_w )
  147. {
  148.     int bankaddress;
  149.     unsigned char *RAM = memory_region(REGION_CPU1);
  150.  
  151.  
  152.     /* bits 0 and 1 are coin counters */
  153.     coin_counter_w(0,data & 1);
  154.     coin_counter_w(1,data & 2);
  155.  
  156.     /* bits 2, 3 and 4 select the ROM bank */
  157.     bankaddress = 0x10000 + (data & 0x1c) * 0x1000;
  158.     cpu_setbank(1,&RAM[bankaddress]);
  159.  
  160.     /* bit 5 resets the sound CPU - we ignore it */
  161.  
  162.     /* bit 6 flips screen */
  163.     if (flipscreen != (data & 0x40))
  164.     {
  165.         flipscreen = data & 0x40;
  166. //        memset(dirtybuffer,1,c1942_backgroundram_size);
  167.     }
  168.  
  169.     /* bit 7 enables characters */
  170.     chon = data & 0x80;
  171. }
  172.  
  173.  
  174.  
  175. WRITE_HANDLER( c1943_d806_w )
  176. {
  177.     /* bit 4 enables bg 1 */
  178.     sc1on = data & 0x10;
  179.  
  180.     /* bit 5 enables bg 2 */
  181.     sc2on = data & 0x20;
  182.  
  183.     /* bit 6 enables sprites */
  184.     objon = data & 0x40;
  185. }
  186.  
  187.  
  188.  
  189. /***************************************************************************
  190.  
  191.   Draw the game screen in the given osd_bitmap.
  192.   Do NOT call osd_update_display() from this function, it will be called by
  193.   the main emulation engine.
  194.  
  195. ***************************************************************************/
  196. void c1943_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
  197. {
  198.     int offs,sx,sy;
  199.     int bg_scrolly, bg_scrollx;
  200.     unsigned char *p;
  201.     int top,left,xscroll,yscroll;
  202.  
  203. /* TODO: support flipscreen */
  204.     if (sc2on)
  205.     {
  206.         p=memory_region(REGION_GFX5)+0x8000;
  207.         bg_scrolly = c1943_bgscrolly[0] + 256 * c1943_bgscrolly[1];
  208.         offs = 16 * ((bg_scrolly>>5)+8);
  209.  
  210.         top = 8 - (bg_scrolly>>5) % 9;
  211.  
  212.         bg_scrolly&=0x1f;
  213.  
  214.         for (sy = 0;sy <9;sy++)
  215.         {
  216.             int ty = (sy + top) % 9;
  217.             unsigned char *map = &sc2map[ty][0][0];
  218.             offs &= 0x7fff; /* Enforce limits (for top of scroll) */
  219.  
  220.             for (sx = 0;sx < 8;sx++)
  221.             {
  222.                 int tile, attr, offset;
  223.                 offset=offs+2*sx;
  224.  
  225.                 tile=p[offset];
  226.                 attr=p[offset+1];
  227.  
  228.                 if (tile != map[0] || attr != map[1])
  229.                 {
  230.                     map[0] = tile;
  231.                     map[1] = attr;
  232.                     drawgfx(sc2bitmap,Machine->gfx[2],
  233.                             tile,
  234.                             (attr & 0x3c) >> 2,
  235.                             attr&0x40, attr&0x80,
  236.                             (8-ty)*32, sx*32,
  237.                             0,
  238.                             TRANSPARENCY_NONE,0);
  239.                 }
  240.                 map += 2;
  241.             }
  242.             offs-=0x10;
  243.         }
  244.  
  245.         xscroll = (top*32-bg_scrolly);
  246.         yscroll = 0;
  247.         copyscrollbitmap(bitmap,sc2bitmap,
  248.             1,&xscroll,
  249.             1,&yscroll,
  250.             &Machine->drv->visible_area,
  251.             TRANSPARENCY_NONE,0);
  252.     }
  253.     else fillbitmap(bitmap,Machine->pens[0],&Machine->drv->visible_area);
  254.  
  255.  
  256.     if (objon)
  257.     {
  258.         /* Draw the sprites which don't have priority over the foreground. */
  259.         for (offs = spriteram_size - 32;offs >= 0;offs -= 32)
  260.         {
  261.             int color;
  262.  
  263.  
  264.             color = spriteram[offs + 1] & 0x0f;
  265.             if (color == 0x0a || color == 0x0b)    /* the priority is actually selected by */
  266.                                                 /* bit 3 of BMPROM.07 */
  267.             {
  268.                 sx = spriteram[offs + 3] - ((spriteram[offs + 1] & 0x10) << 4);
  269.                 sy = spriteram[offs + 2];
  270.                 if (flipscreen)
  271.                 {
  272.                     sx = 240 - sx;
  273.                     sy = 240 - sy;
  274.                 }
  275.  
  276.                 drawgfx(bitmap,Machine->gfx[3],
  277.                         spriteram[offs] + ((spriteram[offs + 1] & 0xe0) << 3),
  278.                         color,
  279.                         flipscreen,flipscreen,
  280.                         sx,sy,
  281.                         &Machine->drv->visible_area,TRANSPARENCY_PEN,0);
  282.             }
  283.         }
  284.     }
  285.  
  286.  
  287. /* TODO: support flipscreen */
  288.     if (sc1on)
  289.     {
  290.         p=memory_region(REGION_GFX5);
  291.  
  292.         bg_scrolly = c1943_scrolly[0] + 256 * c1943_scrolly[1];
  293.         bg_scrollx = c1943_scrollx[0];
  294.         offs = 16 * ((bg_scrolly>>5)+8)+2*(bg_scrollx>>5) ;
  295.         if (bg_scrollx & 0x80) offs -= 0x10;
  296.  
  297.         top = 8 - (bg_scrolly>>5) % 9;
  298.         left = (bg_scrollx>>5) % 9;
  299.  
  300.         bg_scrolly&=0x1f;
  301.         bg_scrollx&=0x1f;
  302.  
  303.         for (sy = 0;sy <9;sy++)
  304.         {
  305.             int ty = (sy + top) % 9;
  306.             offs &= 0x7fff; /* Enforce limits (for top of scroll) */
  307.  
  308.             for (sx = 0;sx < 9;sx++)
  309.             {
  310.                 int tile, attr, offset;
  311.                 int tx = (sx + left) % 9;
  312.                 unsigned char *map = &sc1map[ty][tx][0];
  313.                 offset=offs+(sx*2);
  314.  
  315.                 tile=p[offset];
  316.                 attr=p[offset+1];
  317.  
  318.                 if (tile != map[0] || attr != map[1])
  319.                 {
  320.                     map[0] = tile;
  321.                     map[1] = attr;
  322.                     tile+=256*(attr&0x01);
  323.                     drawgfx(sc1bitmap,Machine->gfx[1],
  324.                             tile,
  325.                             (attr & 0x3c) >> 2,
  326.                             attr & 0x40,attr & 0x80,
  327.                             (8-ty)*32, tx*32,
  328.                             0,
  329.                             TRANSPARENCY_NONE,0);
  330.                 }
  331.             }
  332.             offs-=0x10;
  333.         }
  334.  
  335.         xscroll = (top*32-bg_scrolly);
  336.         yscroll = -(left*32+bg_scrollx);
  337.         copyscrollbitmap(bitmap,sc1bitmap,
  338.             1,&xscroll,
  339.             1,&yscroll,
  340.             &Machine->drv->visible_area,
  341.             TRANSPARENCY_COLOR,0);
  342.     }
  343.  
  344.  
  345.     if (objon)
  346.     {
  347.         /* Draw the sprites which have priority over the foreground. */
  348.         for (offs = spriteram_size - 32;offs >= 0;offs -= 32)
  349.         {
  350.             int color;
  351.  
  352.  
  353.             color = spriteram[offs + 1] & 0x0f;
  354.             if (color != 0x0a && color != 0x0b)    /* the priority is actually selected by */
  355.                                                 /* bit 3 of BMPROM.07 */
  356.             {
  357.                 sx = spriteram[offs + 3] - ((spriteram[offs + 1] & 0x10) << 4);
  358.                 sy = spriteram[offs + 2];
  359.                 if (flipscreen)
  360.                 {
  361.                     sx = 240 - sx;
  362.                     sy = 240 - sy;
  363.                 }
  364.  
  365.                 drawgfx(bitmap,Machine->gfx[3],
  366.                         spriteram[offs] + ((spriteram[offs + 1] & 0xe0) << 3),
  367.                         color,
  368.                         flipscreen,flipscreen,
  369.                         sx,sy,
  370.                         &Machine->drv->visible_area,TRANSPARENCY_PEN,0);
  371.             }
  372.         }
  373.     }
  374.  
  375.  
  376.     if (chon)
  377.     {
  378.         /* draw the frontmost playfield. They are characters, but draw them as sprites */
  379.         for (offs = videoram_size - 1;offs >= 0;offs--)
  380.         {
  381.             sx = offs % 32;
  382.             sy = offs / 32;
  383.             if (flipscreen)
  384.             {
  385.                 sx = 31 - sx;
  386.                 sy = 31 - sy;
  387.             }
  388.  
  389.             drawgfx(bitmap,Machine->gfx[0],
  390.                     videoram[offs] + ((colorram[offs] & 0xe0) << 3),
  391.                     colorram[offs] & 0x1f,
  392.                     flipscreen,flipscreen,
  393.                     8*sx,8*sy,
  394.                     &Machine->drv->visible_area,TRANSPARENCY_COLOR,79);
  395.         }
  396.     }
  397. }
  398.